package cn.seocoo.platform.web.controller.base;

import cn.seocoo.platform.base.domain.BaseCategory;
import cn.seocoo.platform.base.service.IBaseCategoryService;
import cn.seocoo.platform.common.annotation.Log;
import cn.seocoo.platform.common.constant.ClassContants;
import cn.seocoo.platform.common.core.controller.BaseController;
import cn.seocoo.platform.common.core.domain.AjaxResult;
import cn.seocoo.platform.common.core.page.TableDataInfo;
import cn.seocoo.platform.common.enums.BusinessType;
import cn.seocoo.platform.framework.util.ShiroUtils;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.*;

import java.util.Date;
import java.util.List;

/**
 * 车辆类别 信息操作处理
 *
 * @author wujian
 * @date 2019-12-19
 */
@Controller
@RequestMapping("/base/baseCategory")
public class BaseCategoryController extends BaseController {
    private String prefix = "base/baseCategory";

    @Autowired
    private IBaseCategoryService baseCategoryService;


    /**
     * 查询车辆类别列表
     */
    @PostMapping("/list")
    @ResponseBody
    public TableDataInfo list(BaseCategory baseCategory) {
        baseCategory.setDelFlag(ClassContants.NO_DEL_FLAG);
        startPage();
        List<BaseCategory> list = baseCategoryService.selectBaseCategoryList(baseCategory);
        return getDataTable(list);
    }


    /**
     * 新增保存车辆类别
     */
    @Log(title = "车辆类别", businessType = BusinessType.INSERT)
    @PostMapping("/add")
    @ResponseBody
    public AjaxResult addSave(BaseCategory baseCategory) {
        baseCategory.setDelFlag(ClassContants.NO_DEL_FLAG);
        baseCategory.setCreateBy(ShiroUtils.getSysUser().getUserName());
        return toAjax(baseCategoryService.insertBaseCategory(baseCategory));
    }

    /**
     * 修改车辆类别
     */
    @GetMapping("/edit/{id}")
    public String edit(@PathVariable("id") Integer id, ModelMap mmap) {
        BaseCategory baseCategory = baseCategoryService.selectBaseCategoryById(id);
        mmap.put("baseCategory", baseCategory);
        return prefix + "/edit";
    }

    /**
     * 修改保存车辆类别
     */
    @Log(title = "车辆类别", businessType = BusinessType.UPDATE)
    @PostMapping("/edit")
    @ResponseBody
    public AjaxResult editSave(BaseCategory baseCategory) {
        baseCategory.setUpdateTime(new Date());
        baseCategory.setUpdateBy(ShiroUtils.getSysUser().getUserName());
        return toAjax(baseCategoryService.updateBaseCategory(baseCategory));
    }


    /**
     * 新增车型分类
     */
    @GetMapping("/pass/{vehicleId}")
    public String pass(@PathVariable("vehicleId") Integer vehicleId, Model model) {
        model.addAttribute("vehicleId", vehicleId);
        return prefix + "/add";
    }

    @PostMapping("/remove")
    @ResponseBody
    public AjaxResult remove(String ids) {
        BaseCategory baseCategory = baseCategoryService.selectBaseCategoryById(Integer.valueOf(ids));
        baseCategory.setDelFlag(ClassContants.IS_DEL_FLAG);
        return toAjax(baseCategoryService.updateBaseCategory(baseCategory));

    }


}
